package software.hust.com.hust_last.util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;


import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import software.hust.com.hust_last.R;
import software.hust.com.hust_last.bean.MyPackage;
import software.hust.com.hust_last.util.oss.OssFileOperator;
import software.hust.com.hust_last.view.AddTreasureActivity;

/**
 * Created by zwjian on 2017/11/28.
 */

public class MyPackageItem extends RecyclerItem<MyPackage> {

    @BindView(R.id.my_toys_img)
    CircleImageView my_toys_img;
    @BindView(R.id.my_toys_name_tv)
    TextView my_toys_name_tv;
    @BindView(R.id.virtual_toys_num_tv)
    TextView virtual_toys_num_tv;

    private Activity mContext;
    private MyPackage mData;
    private int mType;

    public MyPackageItem(Activity context, ViewGroup parent, int type) {
        super(context, parent);
        mContext = context;
        mType = type;
    }

    @Override
    protected int getLayout() {
        return R.layout.my_toys_style;
    }

    @Override
    protected void bindDataImpl(int position, MyPackage data) {
        reset();
        mData = data;
        OssFileOperator.loadImage(my_toys_img, data.getProp_image());
        my_toys_name_tv.setText(data.getProp_name());
        virtual_toys_num_tv.setText("目前数量：" + data.getProp_num() + "个");
    }

    @OnClick(R.id.my_package_layout)
    public void useToys() {
        if (mType == 1) {
            //将道具作为宝藏
            MyPackage pp = new MyPackage();
            pp.setProp_name(mData.getProp_name());
            pp.setProp_id(mData.getProp_id());
            pp.setProp_num(mData.getProp_num());
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("MyPackage", pp);
            intent.putExtras(bundle);
            mContext.setResult(AddTreasureActivity.GET_VIRTUALTOYS_SUCESS, intent);
            mContext.finish();
        } else if (mType == 0) {
            //地图界面进来，查看自己道具(使用道具)
            String id = String.valueOf(mData.getProp_id());
            Intent intent = new Intent();
            intent.putExtra("prop_id",id);
            mContext.setResult(Activity.RESULT_OK, intent);
            ToastUtil.show(mContext, "你使用了：" + mData.getProp_name());
            mContext.finish();

        }
    }

    private void reset() {
        my_toys_name_tv.setText("");
        virtual_toys_num_tv.setText("");
    }
}
